// pages/Itinerary/searchList/searchList.js
import { GetPlaceListForPage } from '../../api/home'
import { imageURL } from '../../../utils/request'

Page({
  data: {
    placeList: [],
    page: 1,
    rows: 10,
    hasMore: true,
    loading: false,
    keyword: '',
    imageURL:''
  },

  onLoad() {
    this.setData({imageURL})
    this.loadPlaceList()
  },
  goDetails(e){
    const place_id = e.currentTarget.dataset.place_id
    wx.navigateTo({
      url: `/pages/Itinerary/SceneryDetails/SceneryDetails?place_id=${place_id}`,
    })
  },
  // 输入框
  onInput(e) {
    this.setData({
      keyword: e.detail.value
    })
  },

  // 点击搜索
  onSearch() {
    this.setData({
      placeList: [],
      page: 1,
      hasMore: true
    })
    this.loadPlaceList()
  },

  // 加载景点列表
  async loadPlaceList() {
    if (!this.data.hasMore || this.data.loading) return

    this.setData({ loading: true })

    try {
      const res = await GetPlaceListForPage({
        page: this.data.page,
        rows: this.data.rows,
        keyword: this.data.keyword,
        is_hot: '' // 传空字符串
      })

      const newList = Array.isArray(res.data.list) ? res.data.list : []

      // 拼接图片前缀
      newList.forEach(item => {
        if (item.image && !item.image.startsWith('http')) {
          item.image = imageURL + item.image
        }
      })

      this.setData({
        placeList: [...this.data.placeList, ...newList],
        page: this.data.page + 1,
        hasMore: newList.length === this.data.rows,
        loading: false
      })
    } catch (err) {
      console.error('获取景点列表失败:', err)
      this.setData({ loading: false })
    }
  },

  // 上拉触底
  onReachBottom() {
    this.loadPlaceList()
  }
})
